package main

import "strings"

type ShadowInfo struct {
	User    string
	Salt    string
	PassStr string
}

func ParseShadowInfo(shadowLine string) *ShadowInfo {
	shadowLine = strings.TrimSpace(shadowLine)
	blocks := strings.Split(shadowLine, ":")
	if len(blocks) < 3 || blocks[1] == "" {
		return nil
	}
	user := blocks[0]
	passStr := blocks[1]
	blocks = strings.Split(passStr, "$")
	if len(blocks) != 4 || blocks[1] != "6" {
		return nil
	}
	salt := "$" + blocks[1] + "$" + blocks[2]

	info := &ShadowInfo{User: user, Salt: salt, PassStr: passStr}
	return info
}

func ParseShadowContent(content string) []*ShadowInfo {
	var res []*ShadowInfo
	lines := strings.Split(content, "\n")
	for _, oneLine := range lines {
		info := ParseShadowInfo(oneLine)
		if info != nil {
			res = append(res, info)
		}
	}
	return res
}
